/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package eu.fastl.dragster.log.config;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class ColumnConfig {
    @XmlAttribute(name = "type", required = true)
    private String type;
    @XmlAttribute(name = "title")
    private String title;
    @XmlAttribute(name = "required")
    private boolean required;

    @XmlElement(name = "element")
    private final List<ColumnElementConfig> elements = new ArrayList<ColumnElementConfig>();

    public boolean isRequired() {
        return this.required;
    }

    public String getType() {
        return this.type;
    }

    public String getTitle() {
        return this.title;
    }

    /**
     * @throws IllegalStateException
     *             if no or more than one element tag with type=EXECUTION_TYPE
     *             is contained
     * @return the Value of the contained Element tag with type=EXECUTION_TYPE
     */
    public String getContainedExecutionType() {
        String retval = null;
        for (ColumnElementConfig elementConfig : this.elements) {
            if ("EXECUTION_TYPE".equals(elementConfig.getType())) {
                if (retval != null) {
                    throw new IllegalStateException("More than one Execution Type element contained in " + toString());
                }
                retval = elementConfig.getValue();
            }
        }
        if (retval == null) {
            throw new IllegalStateException("No Execution Type element contained in " + toString());
        }
        return retval;
    }

    /**
     * @throws IllegalStateException
     *             if more than one element tag with
     *             type=EXECUTION_DETAIL_CONTAINS is contained
     * @return the Value of the contained Element tag with
     *         type=EXECUTION_DETAIL_CONTAINS or null if none present
     */
    public String getContainedDetailContains() {
        String retval = null;
        for (ColumnElementConfig elementConfig : this.elements) {
            if ("EXECUTION_DETAIL_CONTAINS".equals(elementConfig.getType())) {
                if (retval != null) {
                    throw new IllegalStateException("More than one Detail contains element contained in " + toString());
                }
                retval = elementConfig.getValue();
            }
        }
        return retval;
    }

    public List<ColumnElementConfig> getElements() {
        return this.elements;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
